﻿package display
{
	import display.data.CTextFieldType;
	import display.data.StateType;
	
	import flash.display.Sprite;
	import flash.text.AntiAliasType;
	import flash.text.GridFitType;
	import flash.text.TextFieldAutoSize;
	
	import skinfactory.SkinScaleImage;
	

	public class CTextArea extends Sprite
	{
		private var bg: SkinScaleImage;
		private var textfield: CTextField;
		private var scrollPane: CScrollPane;
		
		public function CTextArea()
		{
			this.initDisplay();
		}
		private function initDisplay(): void
		{
			bg = new SkinScaleImage( "workspace_bg" );
			textfield = new CTextField( CTextFieldType.LIST );
			textfield.defaultTextFormat = CTextFormat.getInstance().area;
			textfield.autoSize = TextFieldAutoSize.LEFT;
			textfield.mouseEnabled = true;
			textfield.multiline = true;
			textfield.selectable = true;
			textfield.wordWrap = true;
			textfield.sharpness = 5;
			textfield.thickness = 5;
			textfield.embedFonts = true
			textfield.antiAliasType = AntiAliasType.ADVANCED;
			textfield.gridFitType = GridFitType.PIXEL;
			
			scrollPane = new CScrollPane();
			scrollPane.child = textfield;
			
			this.setPosition();
			this.registerObj();
		}
		private function registerObj(): void
		{
			if( ! this.contains( bg ) ) 			this.addChild( bg );
			if( ! this.contains( scrollPane ) ) 	this.addChild( scrollPane );
		}
		private function setPosition(): void
		{
			scrollPane.x = bg.x + 5;
			scrollPane.y = 1;
			
			scrollPane.child = textfield; 
		}
		override public function set width(value:Number):void
		{
			bg.width = value;
			textfield.width = value - 6;
			scrollPane.width = textfield.width;
			
			this.setPosition();
		}
		override public function set height(value:Number):void
		{
			bg.height = value;
			scrollPane.height = value - 2;
			this.setPosition();
		}
		override public function get height():Number
		{
			return bg.height;
		}
		
		private var textColor: String;
		public function text ( data: String, type: String ): void
		{
			switch ( type )
			{
				case StateType.DEBUG_ERROR:
					textfield.htmlText += "<font color='#ff3399'>" + data + "</font>" + "\n";
				break;
				
				case StateType.DEBUG_INFO:					
					textfield.htmlText += "<font color='#ff0000'>" + data + "</font>" + "\n";
				break;
				
				case StateType.DEBUG_LOG:
					textfield.htmlText += "<font color='#99cc00'>" + data + "</font>" + "\n";
				break;
				
				case StateType.DEBUG_MEMORY:
					textfield.htmlText += "<font color='#ff0000'>" + data + "</font>" + "\n";
				break;			
				
				case StateType.DEBUG_DEBUG:
					textfield.htmlText += "<font color='##0000FF'>" + data + "</font>" + "\n";
				break;
			}
			
			if ( type == "" )
			{
				textfield.htmlText += "<font color='#00000'>" + data + "</font>" + "\n";
			}
			scrollPane.child = textfield;
			scrollPane.update();
			this.setPosition();
		}
		public function get log(): String
		{
			return textfield.text.toString();
		}
				
	}
}